<?php

namespace App\Jobs;

use App\Http\Service\CountryService;
use App\Model\Business;
use App\Model\House;
use App\Model\TjCountry;
use App\Models\Contract;
use App\Models\MemberCoupon;
use App\Models\Order;
use App\Models\Paper;
use App\services\ImmService;
use App\User;
use App\Utils\Result;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;

class CheckHouse implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $busId;
    public $tries = 3;
    public $timeout = 120;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($busId)
    {
        $this->busId = $busId;
        //因为要导入大文件 需要很久时间
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
//        try{
            Log::info('queue-house:'.$this->busId);
            $business = Business::query()->where('id',$this->busId)->first();
            $user = User::query()->where('id',$business->user_id)->first();
            $houses = House::query()->where('is_push',0)
                ->where('business_id',$this->busId)
                ->get();
            foreach($houses as $house){
                $country = TjCountry::query()->whereNotNull('name')->get();
                foreach ($country as $v) {
                    if ($v->province == $house->province && $v->city == $house->city) {
                        if ($house->district) {
                            if ($v->district == $house->district) {
                                if (strpos($house->estate_name, $v->name) !== false) {
                                    $house->country_code = $v->country_code;
                                    $house->save();
                                    break;
                                }
                            }
                        } else {
                            if (strpos($house->estate_name, $v->name) !== false) {
                                $house->country_code = $v->country_code;
                                $house->save();
                                break;
                            }
                        }
                    }
                }
                if (!$house->country_code) {
                    $country = TjCountry::query()->whereNotNull('country')->get();
                    foreach ($country as $v) {
                        if ($v->province == $house->province && $v->city == $house->city) {
                            if ($house->district) {
                                if ($v->district == $house->district) {
                                    if ($house->country && $house->country == $v->country) {
                                        $house->country_code = $v->country_code;
                                        $house->save();
                                    }
                                }
                            } else {
                                if ($house->country && $house->country == $v->country) {
                                    $house->country_code = $v->country_code;
                                    $house->save();
                                }
                            }
                        }
                    }
                }
                if ($house->country_code) {
                    $countryPush['name'] = $business['name']?$business->name:'未实名';
                    $countryPush['phone'] = $user->mobile;
                    $countryPush['house_id'] = $house->id;
                    $countryPush['address'] = $house->province.$house->city.$house->district.$house->estate_name.$house->name;
                    $countryPush['regionCode'] = $house->country_code;
                    $countryService = new CountryService();
                    $countryService->pushBusiness($countryPush);
                }
            }
//        }catch (\Exception $e){
//            Log::info($e->getMessage());
//        }

    }
}

